package midoo.server.core;

import io.netty.channel.Channel;

import java.net.InetSocketAddress;

/**
 * Created by ajianzheng on 2015/4/10.
 */
public class MMessage {
//    private static AtomicLong counter = new AtomicLong(0);

    private Channel channel;
    private Object message;
//    private long messageID;

    private long processStartTimeMillis;

    public MMessage(Channel channel, Object msg) {
        this.channel = channel;
        this.message = msg;
        this.processStartTimeMillis = System.currentTimeMillis();
    }

    public MMessage() {
        this.processStartTimeMillis = System.currentTimeMillis();
    }

    public MMessage(MMessage message) {
        this.message = message.getMessage();
        this.channel = message.getChannel();
        this.processStartTimeMillis = message.getProcessStartTimeMillis();
    }

    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public Object getMessage() {
        return message;
    }

    public void setMessage(Object message) {
        this.message = message;
    }

    public long getProcessStartTimeMillis() {
        return processStartTimeMillis;
    }

    public void setProcessStartTimeMillis(long processStartTimeMillis) {
        this.processStartTimeMillis = processStartTimeMillis;
    }

    public String getRemoteString() {
        InetSocketAddress address = (InetSocketAddress) channel.remoteAddress();

        return address.getAddress().getHostAddress();
    }

    public long getWaitingMillis() {
        return System.currentTimeMillis() - processStartTimeMillis;
    }

    @Override
    public String toString() {
        if (null != message) {
            return message.toString();
        } else {
            return "";
        }
    }

    public void sendToClient(Object object) {
        this.channel.writeAndFlush(object);
    }
}
